#include "stdafx.h"
#include "PrimitiveShapeManager.h"
#include "RenderSystem.h"
#include "Camera.h"

#include "ShaderManager.h"

void PrimitiveShapeManager::Init(RenderSystem *render_system)
{

}

void PrimitiveShapeManager::CreateCube(int x, int y, int z, int width, int height, int depth, RenderSystem *render_system)
{

	Vertex vertices[]={
		//Top 4 vertices
		Vector3(x - (width / 2), y - (height / 2), z - (depth / 2)), Vector3(x + (width / 2), y - (height / 2), z - (depth / 2)),
		Vector3(x - (width / 2), y - (height / 2), z + (depth / 2)), Vector3(x + (width / 2), y - (height / 2), z + (depth / 2)),
		//Bottom 4 vertices
		Vector3(x - (width / 2), y + (height / 2), z - (depth / 2)), Vector3(x + (width / 2), y + (height / 2), z - (depth / 2)),
		Vector3(x - (width / 2), y + (height / 2), z + (depth / 2)), Vector3(x + (width / 2), y + (height / 2), z + (depth / 2))
	};

	render_system->CreateVertexBuffer(&mRenderable.mVertexBuffer,BM_DEFAULT,width*height,sizeof(Vertex),vertices);
}

void PrimitiveShapeManager::Shut()
{
	mShader.Release();
	mDepthState.Release();
}

void PrimitiveShapeManager::Render(RenderSystem *render_system, class Camera *camera)
{
	using namespace DirectX;
	
	XMFLOAT4X4 rot;
	XMStoreFloat4x4(&rot,camera->GetView());
	rot._41=0.0f; rot._42=0.0f; rot._43=0.0f;
	Mat4 view=XMLoadFloat4x4(&rot);

	OncePerFrame frame;
	frame.proj=camera->GetProj();
	frame.view=view;
	mShader.SetVSConstant(0,sizeof(OncePerFrame),&frame);

	render_system->SelectShader(&mShader);
	//render_system->SelectTexture(&mTexture,0);
	//render_system->SelectIndexBuffer(nullptr);
	render_system->SelectVertexBuffer(&mRenderable.mVertexBuffer,0);
	//render_system->SelectDepthState(&mDepthState);
	render_system->Apply();
	render_system->Draw(PRIM_TRIANGLE_LIST,0,mRenderable.mCount);
}